import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Delete {
    public void deleteBorrowInfo() throws SQLException {
        //1.创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/ebook?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");
        //2.建立连接
        Connection connection = dataSource.getConnection();
        //3.构造sql语句
        String sql = "delete from borrow_info order by id desc limit 1";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //4.执行sql语句
        int n = preparedStatement.executeUpdate();
        System.out.println("n = " + n);
        //5.释放资源
        preparedStatement.close();
        connection.close();
    }
}
